package com.xbongbong.pro.statistic.help.strategy.field.method;

import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.statistic.help.strategy.field.FieldAnalysisStrategy;
import com.xbongbong.pro.statistic.pojo.FieldAnalysisPojo;
import com.xbongbong.pro.statistic.result.pojo.FieldAttrPojo;
import com.xbongbong.saas.domain.entity.PublicGroupSeniorEntity;
import com.xbongbong.saas.model.PublicGroupSeniorModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @description 解析公海分组字段，fieldType 930000
 * @author yanglei
 * @date 2023/4/18 15:53
 */
@Component
public class PublicGroupFieldAnalysis implements FieldAnalysisStrategy {

    @Resource
    private PublicGroupSeniorModel publicGroupSeniorModel;
    @Override
    public Map<String, String> doAnalysisField(FieldAnalysisPojo fieldAnalysisPojo) {
        List<String> values = fieldAnalysisPojo.getValues();
        List<String> valuesResult = fieldAnalysisPojo.getValuesResult();
        FieldAttrPojo field = fieldAnalysisPojo.getField();
        Map<Long, PublicGroupSeniorEntity> publicGroupMap = publicGroupSeniorModel.getPublicGroupMap(field.getFormId(), fieldAnalysisPojo.getCorpid());
        Map<String, String> results = new HashMap<>(1 << 4);
        for (int i = 0; i < values.size(); i++) {
            String groupId = valuesResult.get(i);
            if (StringUtil.isEmpty(groupId) || Objects.equals(groupId,"0")){
                continue;
            }
            results.put(values.get(i), publicGroupMap.getOrDefault(Long.valueOf(groupId),new PublicGroupSeniorEntity()).getName());
        }
        return results;
    }
}
